#!/bin/bash

# The 1st time /usr/bin/iiab-check-firmware runs (at the end of
# firmware/tasks/install.yml) 2-4 lynchpin top links are put in place,
# finalizing symlink chains like:
#
#     /lib/firmware/cypress/X.{bin|blob} ->
#     /lib/firmware/cypress/X.{bin|blob}.iiab ->
#     CHOSEN-FIRMWARE-FILE-OR-LINK
#
# Also backing up top-of-chain originals (file or link!) by moving these to:
#
#     /lib/firmware/cypress/<ORIGINAL FILENAME>.YYYY-MM-DD-HH:MM:SS
#
# NOTE these are "doubly timestamped" to preserve BOTH last-modif & moving date.

# 2023-02-25: bash scripts using default_vars.yml &/or local_vars.yml
# https://github.com/iiab/iiab-factory/blob/master/iiab
# https://github.com/iiab/iiab/blob/master/roles/firmware/templates/iiab-check-firmware#L10-14
# https://github.com/iiab/iiab/blob/master/roles/network/templates/gateway/iiab-gen-iptables#L48-L52
# https://github.com/iiab/maps/blob/master/osm-source/pages/viewer/scripts/iiab-install-map-region#L23-L39
# https://github.com/iiab/iiab/blob/master/roles/0-DEPRECATED-ROLES/openvpn/templates/iiab-support READS AND WRITES, INCL NON-BOOLEAN

iiab_var_value() {
    v1=$(grep "^$1:\s" /opt/iiab/iiab/vars/default_vars.yml | tail -1 | sed "s/^$1:\s\+//; s/#.*//; s/\s*$//; s/^\(['\"]\)\(.*\)\1$/\2/")
    v2=$(grep "^$1:\s" /etc/iiab/local_vars.yml | tail -1 | sed "s/^$1:\s\+//; s/#.*//; s/\s*$//; s/^\(['\"]\)\(.*\)\1$/\2/")
    [ "$v2" != "" ] && echo $v2 || echo $v1    # [ "$v2" ] ALSO WORKS
}

link_fw() {
    if [[ $(readlink /lib/firmware/cypress/$1) != $1.iiab ]] ; then
        echo
        mv /lib/firmware/cypress/$1 /lib/firmware/cypress/$1.$(date +%F-%T)
        ln -s $1.iiab /lib/firmware/cypress/$1
        echo -e "\e[1mSymlinked /lib/firmware/cypress/$1 -> $1.iiab\e[0m"
        touch /tmp/.fw_modified
    fi
}

if [[ $(iiab_var_value rpi3bplus_rpi4_wifi_firmware) != "os" ]] ; then
    link_fw cyfmac43455-sdio.bin
    link_fw cyfmac43455-sdio.clm_blob
fi

if [[ $(iiab_var_value rpizerow_rpi3_wifi_firmware) != "os" ]] ; then
    link_fw cyfmac43430-sdio.bin
    link_fw cyfmac43430-sdio.clm_blob
fi

if [ -f /tmp/.fw_modified ]; then
    bash /etc/profile.d/iiab-firmware-warn.sh
else
    echo -e "\n\e[1mWiFi Firmware links in /lib/firmware/cypress appear \e[92mCORRECT\e[0m\e[1m, per iiab/iiab#3482\e[0m"
    echo
    echo -e "\e[100;1m(No reboot appears necessary!)\e[0m"
    echo
    echo -e "NOTE: If you change rpi3bplus_rpi4_wifi_firmware or rpizerow_rpi3_wifi_firmware"
    echo -e "settings in /etc/iiab/local_vars.yml, please then run:"
    echo
    echo -e "  cd /opt/iiab/iiab"
    echo -e "  sudo iiab-hotspot-off    # NO LONGER NEC? eg to restore 'wifi_up_down: True'"
    echo -e "  sudo ./runrole --reinstall firmware"
    echo -e "  sudo iiab-network        # SOMETIMES NECESSARY"
    echo -e "  sudo iiab-hotspot-on     # NO LONGER NEC? eg to restore 'wifi_up_down: True'"
    echo -e "  sudo reboot\n"
    #echo
    #echo -e "Disconnect your power cord before rebooting, for better WiFi firmware results.\n"
fi

# \e[1m = bright white    \e[100;1m = bright white, on gray    \n\e[41;1m = bright white, on red
# \e[42;1m = bright white, on bright green    \e[92m = green on black
